using System;
using System.Collections.Generic;

namespace Common.Utilities
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Matrix<T>
    {
        private int _RowCount;
        private int _ColumnCount;
        private List<string> _RowLabels;
        private List<string> _ColumnLabels;
        private T[,] _Data;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="rowCount"></param>
        /// <param name="colCount"></param>
        public Matrix(int rowCount, int colCount)
        {
            this._RowCount = rowCount;
            this._ColumnCount = colCount;
            this._RowLabels = new List<string>();
            this._ColumnLabels = new List<string>();
            for (int i = 0; i < this._RowCount; i++)
            {
                this._RowLabels.Add("Row_" + i);
            }
            for (int i = 0; i < this._ColumnCount; i++)
            {
                this._ColumnLabels.Add("Column_" + i);
            }
            this._Data = new T[this._RowCount,this._ColumnCount];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="colNames"></param>
        /// <param name="rowNames"></param>
        public Matrix(string[] colNames, string[] rowNames)
        {
            this._RowCount = rowNames.Length;
            this._ColumnCount = colNames.Length;
            this._RowLabels = new List<string>();
            this._ColumnLabels = new List<string>();
            for (int i = 0; i < rowNames.Length; i++)
            {
                this._RowLabels.Add(rowNames[i]);
            }
            for (int i = 0; i < colNames.Length; i++)
            {
                this._ColumnLabels.Add(colNames[i]);
            }
            this._Data = new T[this._RowCount, this._ColumnCount];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <param name="rowData"></param>
        public void AddRow(int rowIndex, T[] rowData)
        {
            if (rowData != null && rowData.Length > 0)
            {
                int length = Math.Min(rowData.Length, this._ColumnCount);
                for (int i = 0; i < length; i++)
                {
                    this._Data[rowIndex, i] = rowData[i];
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <returns></returns>
        public T this[int row, int col]
        {
            get
            {
                return this._Data[row, col];
            }
        }
    }
}
